package com.sense.kukoo.lib_grant;

import android.app.Activity;

import java.util.LinkedList;
import java.util.List;

import io.reactivex.rxjava3.functions.Consumer;

/**
 * <br> ClassName:   MyPermissionCallback
 * <br> Description: 对权限库的回调进行二次封装，处理权限申请未通过的UI交互
 * <p>
 * <br> Author:      lionszhang
 * <br> Date:        2018/2/28
 */
public abstract class MyPermissionCallback
        implements PermissionCallback, Consumer<List<PermissionResult>> {

    private Activity mActivity;
    private List<String> mRefusedPermissions;
    private List<String> mNoAskPermissions;

    public MyPermissionCallback(Activity activity) {
        mActivity = activity;
    }


    @Override public void accept(List<PermissionResult> permissionResults) throws Exception {
        LinkedList<String> mRefusedList = new LinkedList<>();
        LinkedList<String> mNoAskList = new LinkedList<>();
        //
        for (PermissionResult result : permissionResults) {
            switch (result.getType()) {

                case DENIED:
                    mRefusedList.add(result.getName());
                    break;

                case NO_ASK:
                    mNoAskList.add(result.getName());
                    break;
            }
        }
        //
        if ((mRefusedList.size() + mNoAskList.size()) == 0) {
            onAllAllow();
        } else {
            //统一按拒绝处理
            mRefusedList.addAll(mNoAskList);
            onRequestFaild(mRefusedList);
        }
    }

    @Override public void onHasRefusedPermissions(List<String> permissions) {
        mRefusedPermissions = permissions;
        if(mNoAskPermissions != null){
            //统一按拒绝处理
            mRefusedPermissions.addAll(mNoAskPermissions);
            onRequestFaild(mRefusedPermissions);
        }
    }

    @Override public void onHadNoAskPermissions(List<String> permissions) {
        mNoAskPermissions = permissions;
        if(mRefusedPermissions != null){
            //统一按拒绝处理
            mRefusedPermissions.addAll(mNoAskPermissions);
            onRequestFaild(mRefusedPermissions);
        }
    }

    /**
     * <br> Description: 权限申请未通过
     * <br> Author:      lionszhang
     * <br> Date:        2018/2/28 16:02
     */
    public void onRequestFaild(List<String> permissions) {
//        String message = PermissionMessageUtils.getPermissionMessage(permissions);
//        Toast.makeText(mActivity, message + "权限申请失败!", Toast.LENGTH_SHORT).show();
    }
}
